<?php

namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

/**
 * Site controller
 */
class SiteController extends \backend\components\Controller {

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow'   => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow'   => true,
                        'roles'   => ['@'],
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionError()
    {
        
        $exception = Yii::$app->errorHandler->exception;
        if ($exception !== null)
        {
            $data = ['code'=> 0, 'name' => '', 'message' => $exception->getMessage(), 'exception' => $exception];
            switch ($exception->statusCode)
            {
                case 404:
                    $view = '404';
                    $data['name'] = $exception->getName();
                    break;;
                case 500:
                    $view = '500';
                    $data['name'] = $exception->getName();
                    break;
                default :
                    $view = 'error';
                    break;
            }
            
            if (Yii::$app->user->isGuest)
            {
                $this->layout = 'error';
                $view = 'error';
            }
            
            return $this->render($view, $data);
        }
    }

}
